﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.ServiceModel;
using WCFServiceWebRole1.Services;
using System.Threading;
using System.Net.Http;
using System.Net.Http.Formatting;


namespace WCFServiceWebRole1_Tests
{
    [TestClass]
    public class UnitTest2
    {
        private static ServiceHost myServiceHost_;

        [ClassInitialize]
        public static void InitializeClass(TestContext ctx)
        {
            UnitTest2.myServiceHost_ = new ServiceHost(typeof(user));
            UnitTest2.myServiceHost_.Open();
  
        }
        [ClassCleanup]
        public static void CleanupClass()
        {
            UnitTest2.myServiceHost_.Close();
        }

        [TestMethod]
        public void TestMethod1()
        {
            using (HttpClient client = new HttpClient())
            {
                client.BaseAddress = new Uri("http://ipv4.fiddler:65000");
                client.DefaultRequestHeaders.Accept.Add(
                    new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

                var input = new { id = 1, name = "Ah Beng", score = 50 };

                ObjectContent content = new ObjectContent(input.GetType(), input, new JsonMediaTypeFormatter());

                HttpResponseMessage response = client.PostAsync("ResultService.svc/SubmitResult", content).Result;

                Assert.IsTrue(response.IsSuccessStatusCode, "Failed to call WCF service.");

                if (response.IsSuccessStatusCode)
                {
                    Assert.AreEqual("{\"SubmitResultResult\":\"Passed\"}", response.Content.ReadAsStringAsync().Result, "Wrong result.");
                }
            }



            //ChannelFactory<Iusers> fac = new ChannelFactory<Iusers>("local");
            //var proxy = fac.CreateChannel();
            //using (proxy as IDisposable)
            //{

            //    var response = proxy.getUserByUserId("1092");



            //    Assert.AreEqual<string>("ayyappan", response.response.Username);

            //}
        }
    }
}
